package com.betty.cscams.controller;

import com.betty.cscams.common.http.HttpResult;
import com.betty.cscams.common.page.PageRequest;
import com.betty.cscams.constant.SysConstants;
import com.betty.cscams.entity.BusTerm;
import com.betty.cscams.entity.PsyQuestion;
import com.betty.cscams.service.BusTermService;
import com.betty.cscams.service.PsyQuestionService;
import com.betty.cscams.service.SysFileService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * ---------------------------
 *  心理测评题目Controller(PsyQuestionController)         
 * ---------------------------
 */
@RestController
@RequestMapping("psyQuestion")
public class PsyQuestionController {

    @Autowired
    private PsyQuestionService psyQuestionService;
    
    @Autowired
    private BusTermService busTermService;
    
    @Autowired
    private SysFileService fileService;

    /**
     * 保存题目
     * @param record 题目记录
     * @return 操作结果
     */    
    @PostMapping(value="/save")
    public HttpResult save(@RequestBody PsyQuestion record) {
        // 检查学期是否有效
        BusTerm busTerm = busTermService.findById(record.getTermId());
        if(ObjectUtils.isEmpty(busTerm) || !SysConstants.TERM_STATUS_2.equals(busTerm.getStatus())){
            return HttpResult.error("当前学期未开始或已结束");
        }
        
        return HttpResult.ok(psyQuestionService.save(record));
    }

    /**
     * 删除题目
     * @param records 题目列表
     * @return 操作结果
     */
    @PostMapping(value="/delete")
    public HttpResult delete(@RequestBody List<PsyQuestion> records) {
        return HttpResult.ok(psyQuestionService.delete(records));
    }

    /**
     * 分页查询题目
     * @param pageRequest 分页请求
     * @return 分页结果
     */    
    @PostMapping(value="/findPage")
    public HttpResult findPage(@RequestBody PageRequest pageRequest) {
        return HttpResult.ok(psyQuestionService.findPage(pageRequest));
    }
    
    /**
     * 根据ID查询题目
     * @param id 题目ID
     * @return 题目信息
     */ 	
    @GetMapping(value="/findById")
    public HttpResult findById(@RequestParam Long id) {
        return HttpResult.ok(psyQuestionService.findById(id));
    }
    
    /**
     * 根据学期ID查询题目列表
     * @param termId 学期ID
     * @return 题目列表
     */
    @GetMapping(value="/findByTermId")
    public HttpResult findByTermId(@RequestParam Long termId) {
        return HttpResult.ok(psyQuestionService.findByTermId(termId));
    }
    
    /**
     * 导入题目
     * @param file Excel文件
     * @param termId 学期ID
     * @return 操作结果
     */
    @PostMapping(value="/importQuestions")
    public HttpResult importQuestions(MultipartFile file, Long termId) throws Exception {
        // 检查学期是否有效
        BusTerm busTerm = busTermService.findById(termId);
        if(ObjectUtils.isEmpty(busTerm) || !SysConstants.TERM_STATUS_2.equals(busTerm.getStatus())){
            return HttpResult.error("当前学期未开始或已结束");
        }
        
        // 导入题目到该学期
        List<PsyQuestion> questions = fileService.importExcelForBean(file, PsyQuestion.class);
        int count = psyQuestionService.importQuestions(questions, termId);
        
        return HttpResult.ok("成功导入 " + count + " 道题目");
    }
} 